var express = require('express')
var md5 = require('blueimp-md5')
var sd = require('silly-datetime')
var multer = require('multer')
var fs = require('fs')
var mysql = require('./database.js')

var api = express.Router()

/* 注册请求 */
api.post('/registerAPI', function (req, res) {
    var body = req.body
    var email = body.email
    body.password = md5(body.password)
    var password = body.password

    var query = 'SELECT * FROM users WHERE email=' + mysql.escape(email)

    mysql.query(query, function (err, data) {
        if (err) {
            res.status(500).json({
                status: 200,
                msg: '数据库错误!'
            })
            return
        }

        if (data.length == 0) {
            var query = 'INSERT users SET email=' + mysql.escape(email) + ',password=' + mysql.escape(password)
            mysql.query(query, function (err, data) {
                if (err) {
                    res.status(500).json({
                        status: 200,
                        msg: '数据库错误!'
                    })
                    return
                }

                res.status(200).json({
                    status: 200,
                    msg: '注册成功,请登录!'
                })
            })
        }
        else {
            res.status(200).json({
                status: 200,
                msg: '邮箱已存在,请登录!',
                1: {
                    city: "上海",
                    AQI: "81",
                    quality: "良",
                    date: "2019-03-05"
                }
            })
        }
    })
})

module.exports = api